<template>
  <div>
    <v-card>
      <v-card-title>
        <v-btn color="primary" @click="addBrand()">新增</v-btn>

        <v-spacer/>

        <v-text-field label="我是来占位的" v-model="search" append-icon="search" hide-details></v-text-field>
      </v-card-title>

      <v-divider/>

      <v-data-table
        :headers="headers"
        :items="brands"
        :pagination.sync="pagination"
        :loading="loading"
        class="elevation-1"
        :total-items="totalBrands"
      >
        <template slot="items" slot-scope="props">
          <td>{{ props.item.id }}</td>
          <td class="text-xs-right">{{ props.item.name }}</td>
          <td class="text-xs-right"><img :src="props.item.image"/></td>
          <td class="text-xs-right">{{ props.item.letter }}</td>
          <td class="justify-center layout">
            <v-btn color="info">编辑</v-btn>
            <v-btn color="warning" @click="deleteBrand(props.item.id)">删除</v-btn>
          </td>
        </template>
      </v-data-table>

    </v-card>
    <!--以下为dialog弹窗-->
    <v-dialog v-model="show" max-width="500" persistent>
      <v-card>
        <!--对话框的标题-->
        <v-toolbar dense dark color="primary">
          <v-toolbar-title>新增品牌</v-toolbar-title>
          <v-spacer/>
          <v-btn icon @click="closeDial()"><v-icon>close</v-icon></v-btn>
        </v-toolbar>
        <!--对话框的内容，表单-->
        <v-card-text class="px-5">
          <my-brand-form  @close="closeDial"></my-brand-form>
        </v-card-text>


      </v-card>


    </v-dialog>


  </div>
</template>

<script>
  // 导入自定义的表单组件
  import MyBrandForm from './MyBrandForm'
  export default {
    name: "my-brand",
    data() {
      return {
        search: '', // 搜索过滤字段
        totalBrands: 0, // 总条数
        brands: [], // 当前页品牌数据
        loading: true, // 是否在加载中
        pagination: {}, // 分页信息
        headers: [ // 头信息
          {text: 'id', align: 'center', value: 'id'},
          {text: '名称', align: 'center', sortable: false, value: 'name'},
          {text: 'LOGO', align: 'center', sortable: false, value: 'image'},
          {text: '首字母', align: 'center', value: 'letter', sortable: false,},
          {text: '操作', align: 'center', value: 'id', sortable: false}
        ],
        show: false
      }
    },
    mounted() { // 挂载后执行
      // 查询数据
      this.getDataFromServer();
    },
    methods: {
      getDataFromServer() {
        this.$http.get("/item/brand/page", {
          params: {
            key: this.search, // 搜索条件
            page: this.pagination.page,// 当前页
            rows: this.pagination.rowsPerPage,// 每页大小
            sortBy: this.pagination.sortBy,// 排序字段
            desc: this.pagination.descending// 是否降序
          }
        })
          .then(({data}) => {
            console.log(data);
            this.brands = data.items;
            this.totalBrands = data.total;
            // 完成赋值后，把加载状态赋值为false
            this.loading = false;
          })
      },
      deleteBrand(id) {

        this.$message.confirm('此操作将永久删除数据，是否继续?', '提示', {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete("/item/brand/" + id)
            .then(() => {
              this.getDataFromServer();
              this.$message.warning('已删除成功');
            }).catch(() => {
            this.$message.error('已取消删除');
          })
        }).catch(() => {
          this.$message.info('已取消删除');
        })
      },
      addBrand() {
        this.show = true;
      },
      closeDial(){
        this.show = false;

        // 重新加载数据
        this.getDataFromServer();
      }
    },
    watch: {
      pagination: {
        deep: true,
        handler() {
          this.getDataFromServer();
        }
      },
      search() {
        this.getDataFromServer();
      }
    },
    components:{
      MyBrandForm
    }
  }
</script>

<style scoped>

</style>
